package com.ehl.capture.tool;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.util.List;

public class ReflectUtil
{
    public static String getJsonByObject(List<Object> lst)
    {
        Class clazz = null;
        StringBuffer sb = new StringBuffer();
        Object o = null;

        if ((lst != null) && (lst.size() > 0)) {
            o = lst.get(0);
            try {
                clazz = Class.forName(o.getClass().toString().substring(6));
            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();
            }
            String nameStr = (clazz != null) ? clazz.getName() : "";
            String clazzName = (!"".equals(nameStr)) ? nameStr.substring(nameStr.lastIndexOf(".") + 1) : "";
            sb.append("\"" + clazzName + "\":" + "[");
            int j = 0; for (int n = lst.size(); j < n; ++j) {
                o = lst.get(j);
                try {
                    clazz = Class.forName(o.getClass().toString().substring(6));
                    Field[] fields = clazz.getDeclaredFields();
                    nameStr = clazz.getName();
                    clazzName = nameStr.substring(nameStr.lastIndexOf(".") + 1);
                    sb.append("{");
                    int i = 0; for (int k = fields.length; i < k; ++i) {
                        String field = fields[i].getName();
                        String method = field.substring(0, 1).toUpperCase() + field.substring(1);
                        Method m = clazz.getMethod("get" + method, new Class[0]);
                        String mVal = StringHelper.obj2str(String.valueOf(m.invoke(o, new Object[0])));
                        if ((mVal == null) || ("null".equals(mVal))) {
                            mVal = "";
                        }
                        sb.append("\"" + field + "\":\"" + mVal + "\"");
                        if (i != k - 1) {
                            sb.append(",");
                        }
                    }
                    sb.append("}");
                    if (j == n - 1)
                        sb.append("]");
                    else
                        sb.append(",");
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    public static Method getGetMethod(Class objectClass, String fieldName)
    {
        StringBuffer sb = new StringBuffer();
        sb.append("get");
        sb.append(fieldName.substring(0, 1).toUpperCase());
        sb.append(fieldName.substring(1));
        try {
            return objectClass.getMethod(sb.toString(), new Class[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Method getSetMethod(Class objectClass, String fieldName)
    {
        try
        {
            Class[] parameterTypes = new Class[1];
            Field field = objectClass.getDeclaredField(fieldName);
            parameterTypes[0] = field.getType();
            StringBuffer sb = new StringBuffer();
            sb.append("set");
            sb.append(fieldName.substring(0, 1).toUpperCase());
            sb.append(fieldName.substring(1));
            Method method = objectClass
                    .getMethod(sb.toString(), parameterTypes);
            return method;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void invokeSet(Object o, String fieldName, Object value)
    {
        Method method = getSetMethod(o.getClass(), fieldName);
        try {
            method.invoke(o, new Object[] { value });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String reflectType(Object object, String fieldName) throws SecurityException, NoSuchFieldException {
        Class[] parameterTypes = new Class[1];
        Field field = object.getClass().getDeclaredField(fieldName);
        parameterTypes[0] = field.getType();
        String strType = parameterTypes[0].toString().substring(parameterTypes[0].toString().lastIndexOf(".") + 1);
        return strType.toLowerCase();
    }

    public static Object invokeGet(Object object, String fieldName)
    {
        Method method = getGetMethod(object.getClass(), fieldName);
        try {
            return method.invoke(object, new Object[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object reflectObject(Class clazz, ResultSet rs)
            throws Exception
    {
        Object obj = clazz.newInstance();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            if (!name.startsWith("serialVersionUID")) {
                Class type = field.getType();
                String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
                Method methodSet = clazz.getDeclaredMethod(strSet, new Class[] { type });
                String strType = type.toString().substring(type.toString().lastIndexOf(".") + 1).toLowerCase();
                if (strType.indexOf(";") == -1) {
                    if (strType.equals("date"))
                        try {
                            methodSet.invoke(obj, new Object[] { rs.getTimestamp(name) });
                        }
                        catch (Exception localException1) {
                        }
                    else if (strType.equals("int"))
                        methodSet.invoke(obj, new Object[] { Integer.valueOf(rs.getInt(name)) });
                    else if (strType.equals("long"))
                        methodSet.invoke(obj, new Object[] { Long.valueOf(rs.getLong(name)) });
                    else
                        try {
                            methodSet.invoke(obj, new Object[] { StringHelper.obj2str(rs.getString(name), "") });
                        } catch (Exception e) {
                            try {
                                methodSet.invoke(obj, new Object[] { "" });
                            }
                            catch (Exception localException2)
                            {
                            }
                        }
                }
            }
        }
        return obj;
    }

    public static Object reflectObject(Object object, ResultSet rs)
            throws Exception
    {
        Class clazz = Class.forName(object.getClass().toString().substring(6));
        Object obj = clazz.newInstance();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            Class type = field.getType();
            String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
            Method methodSet = clazz.getDeclaredMethod(strSet, new Class[] { type });
            String strType = type.toString().substring(type.toString().lastIndexOf(".") + 1).toLowerCase();
            if (strType.equals("date"))
                methodSet.invoke(obj, new Object[] { rs.getTimestamp(name) });
            else if (strType.equals("int"))
                methodSet.invoke(obj, new Object[] { Integer.valueOf(rs.getInt(name)) });
            else if (strType.equals("long"))
                methodSet.invoke(obj, new Object[] { Long.valueOf(rs.getLong(name)) });
            else {
                methodSet.invoke(obj, new Object[] { StringHelper.obj2str(rs.getString(name)) });
            }
        }
        return obj;
    }

    public static String reflectInsertSql(String tableName, List<Object> list, Object object)
            throws Exception
    {
        StringBuffer columnSql = new StringBuffer("insert into " + tableName + "(");
        StringBuffer valSql = new StringBuffer("values(");
        Class clazz = Class.forName(object.getClass().toString().substring(6));
        Method[] methods = clazz.getMethods();
        boolean bl = false;
        for (int i = 0; i < methods.length; ++i) {
            Method method = methods[i];
            if (method.getName().startsWith("get")) {
                String filed = method.getName().substring(3);
                for (int j = 0; j < list.size(); ++j) {
                    TableBean bean = (TableBean)list.get(j);
                    if (filed.toLowerCase().equals(bean.getColumn_name().toLowerCase())) {
                        String data_type = bean.getData_type().toLowerCase();
                        Object value = method.invoke(object, new Object[0]);

                        if (value == null) break;
                        bl = true;
                        if ((!data_type.startsWith("date")) && (value.toString().length() > bean.getData_length())) break;
                        columnSql.append(bean.getColumn_name().toLowerCase()).append(",");
                        if (data_type.indexOf("char") != -1) {
                            valSql.append("'" + value + "'").append(",");
                            break; } if (data_type.startsWith("date")) {
                            if (value.toString().length() == 10) {
                                if (value.toString().indexOf("-") != -1) {
                                    valSql.append("to_date('" + value + "','yyyy-mm-dd')").append(",");
                                    break;
                                }valSql.append("to_date('" + value + "','yyyy/mm/dd')").append(",");

                                break;
                            }if (value.toString().indexOf("-") != -1) {
                                valSql.append("to_date('" + value + "','yyyy-mm-dd hh24:mi:ss')").append(",");
                                break;
                            }valSql.append("to_date('" + value + "','yyyy/mm/dd hh24:mi:ss')").append(",");

                            break; } if (!data_type.startsWith("number")) break;
                        valSql.append("'" + value + "'").append(",");

                        break;
                    }
                }
            }
        }
        String sql = columnSql.substring(0, columnSql.length() - 1) + ")" + valSql.substring(0, valSql.length() - 1) + ")";
        if (!bl) {
            return "";
        }
        return sql;
    }
}